#-------------------------------------------------------------------------------
#TODO: file information
#
#
#-------------------------------------------------------------------------------

import mod_pylas, os,mod_pylas_ogr,tscan_prj_header

outDir = ""
while not os.path.exists(outDir):
    outDir = raw_input("Output path for the prj file: ")
    if len(outDir) > 0 and outDir[-1] != "\\":
        outDir = outDir + "\\"
## DONE: Verify output directory from user
## print "\nDEBUG: " + "tscan file output directory (with '\\'): " + outDir + "\n"
    
outName = ""
while len(outName) <= 0 or outName is None:
    outName = raw_input("Output name for the prj file (default: 'tscan_project'): ")
    
    if outName == "" or outName is None or len(outName) < 1:
        outName = "tscan_project"
## DONE: Verify output from user entered name
## print "\nDEBUG: " + "tscan file output name (from user input): " + outName + "\n"
## DONE: Verify output from default name
## print "\nDEBUG: " + "tscan file output name (default): " + outName + "\n"

prjName = outDir + outName + ".prj"
prjFile = open(prjName,'w')
## DONE: Verify final name user input
## DONE: Verify final name default option
## print "\nDEBUG: " + "tscan file output name (full path, default name): " + prjName + "\n"

if prjFile is not None:
    ## Generate header code
    for headitem in tscan_prj_header.prj_head_list:
        if (headitem[0] == 'projid'):
            prjFile.write(headitem[1])
            ## DONE: print tscan project identifier headitem[1]
            ## print "\nDEBUG: " + "tscan project identifier headitem[1]: " + headitem[1] + "\n"
        else:
            prjFile.write(headitem[0] + "=" + headitem[1])
            ## DONE: print headitem[0] + "=" + headitem[1]
            ## print "\nDEBUG: " + "tscan header item: " + headitem[0] + "=" + headitem[1] + "\n"
        prjFile.write("\n")    
    prjFile.write("\n")
    
    ## Read from shapefile or LAS directory
    inPath = ""
    while not os.path.exists(inPath):
        inPath = raw_input("Enter the path to a directory or shapefile: ")
        ## DONE: enter path that does not exist
        ## print "\nDEBUG: " + "LAS input path: " + inPath + "\n"

    if os.path.isdir(inPath):
        ## DONE: indicate that this is a directory
        ## print "\nDEBUG: " + "LAS directory path: " + inPath + "\n"
        if inPath[-1] != "\\":
            inPath = inPath + "\\"
        ## DONE: verify that a directory has the trailing slash added
        ## print "\nDEBUG: " + "LAS directory path (with trailing slash): " + inPath + "\n"

        ## Code for LAS reading
        listLAS = mod_pylas.getLASList(inPath)
        ## DONE: Show the list of LAS files
        ## print "\nDEBUG: " + "Show LAS directory file list" + str(listLAS) + "\n"
        
        for las in listLAS:
            print "Reading: " + las
            inLas = inPath + las
            ## DONE: Show the LAS file full path 
            ## print "\nDEBUG: " + "    Show the LAS file full path: " + inLas + "\n"

            ## block name
            prjFile.write("Block " + inLas + "\n")
            ## DONE: Show the block header 
            ## print "\nDEBUG: " + "    Show the block header: " + "Block " + inLas + "\n" + "\n"
            
            inHead = mod_pylas.parseHeader(inLas)
            ## DONE: Show the LAS header from pylas
            ## print "\nDEBUG: " + "    Show the LAS header (from pylas): " + str(inHead) + "\n"

            ## Bounding coordinates
            prjFile.write(str(inHead['xmin']) + " " + str(inHead['ymax']) + "\n")
            prjFile.write(str(inHead['xmax']) + " " + str(inHead['ymax']) + "\n")
            prjFile.write(str(inHead['xmax']) + " " + str(inHead['ymin']) + "\n")
            prjFile.write(str(inHead['xmin']) + " " + str(inHead['ymin']) + "\n")
            prjFile.write("\n")
    elif os.path.isfile(inPath):
        ## TODO: indicate that this is a shapefile
        print "\nDEBUG: " + "Shapefile path: " + inPath + "\n"
        ext = os.path.splitext(inPath)[1]
        if ext.upper().find('SHP'): 
            
            listFieldNames = mod_pylas_ogr.getFieldNamesFromPath(inPath)
            ind = 0
            for fldName in listFieldNames:
                print str(ind) +  " " + fldName
                ind = ind + 1
                
            choice = -1
            while choice == -1:
                choice = raw_input("LAS name field: ")
                print len(listFieldNames)
                try:
                    choice = int(choice)
                except:
                    choice = -1
                    continue
                    
                if choice < 0 or choice > len(listFieldNames):
                    choice = -1
                    continue
            
            choice2 = raw_input("LAS directory path field (default: no directory, -1: enter directory): ")
            try:
                choice2 = int(choice2)
                if choice2 > len(listFieldNames):
                    print 'Field index out of range'
            except:
                print 'Unacceptable input'
            
            lasPath = ""    
            if choice2 == -1:
                ## Read from shapefile or LAS directory
                while not os.path.exists(lasPath):
                    lasPath = raw_input("Enter the directory path: ") 
                
                if lasPath[-1] != "\\":
                    lasPath = lasPath + "\\"
            
            attList = []
            if choice2 == -1:
                attList = [listFieldNames[choice]]
            if choice2 > -1:
                attList = [listFieldNames[choice],listFieldNames[choice2]]
            print attList
            
            tileInfo = mod_pylas_ogr.getFeatureBoundingAndAttributeDataFromPath(inPath,attList)
            for tile in tileInfo:
                print "Reading: " + tile
                if choice2 > -1:
                    ## block name
                    lasName = tile[1][0][listFieldNames[choice]]
                    print listFieldNames[choice2] + " " + str(choice2)
                    lasDir = tile[1][0][listFieldNames[choice2]]
                    if (len(lasDir) > 0 and lasDir[-1] != "\\"):
                        lasDir = lasDir + "\\" 
                    lasFullPath = lasDir + lasName
                    
                else:
                    lasFullPath = lasPath + tile[1][0][listFieldNames[choice]]
                    
                ## block name
                prjFile.write("Block " + lasFullPath + "\n")
                
                ## Bounding coordinates
                # minx, maxx, miny, maxy
                prjFile.write(str(tile[0][0]) + " " + str(tile[0][3]) + "\n")
                prjFile.write(str(tile[0][1]) + " " + str(tile[0][3]) + "\n")
                prjFile.write(str(tile[0][1]) + " " + str(tile[0][2]) + "\n")
                prjFile.write(str(tile[0][0]) + " " + str(tile[0][2]) + "\n")
                prjFile.write("\n")
                
    print "tscan project generation completed"
    
prjFile.close()
